home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 2000 January
/
Macworld (2000-01).dmg
/
Shareware World
/
Info
/
For Developers
/
OSA Script.sea
/
OSA Script
/
ExecuteEventInScript.p
< prev
next >
Wrap
Text File
|
1999-09-27
|
4KB
|
90 lines
unit ExecuteEventInScript;
{©23/7/99 — Jan Skarbek. http://www.ozemail.com.au/~pbird. pbird@ozemail.com.au}
{You are welcome to distribute this AppleEvent handling sample freely, but must keep the package intact.}
{Comments, questions, and suggestions invited.}
{Formatting based on Ingemar Ragnemalm's pi_script.p... which is based... :) }
{See ExecuteScript.p for more details}
interface
uses
Processes, Resources, AppleEvents, Components, OSA;
function ExecuteEventInCompiledScriptResource (scriptResID: Integer;
sentData: AEDesc;
var replyData: AEDesc;
class, ID: DescType): OSErr;
implementation
function ExecuteEventInCompiledScriptResource (scriptResID: Integer;
sentData: AEDesc;
var replyData: AEDesc;
class, ID: DescType): OSErr;
var
iErr, ie: OSErr;
ignoreErr: OSAError;
scriptData: Handle;
ae, reply: AppleEvent;
scriptDesc, addressDesc: AEDesc;
scriptID: OSAID;
myScriptingComponent: ComponentInstance;
myPSN: ProcessSerialNumber;
begin
ae.dataHandle := nil;
reply.dataHandle := nil;
addressDesc.dataHandle := nil;
{get script from resource}
scriptData := GetResource('scpt', scriptResID);
iErr := ResError;
if (iErr = noErr) and (scriptData = nil) then
iErr := ResNotFound;
if iErr = noErr then
begin
{load the script data}
scriptDesc.descriptorType := typeOSAGenericStorage;
scriptDesc.dataHandle := scriptData;
{open a connection with AppleScript component}
{NOTE that is you do this often, you should use a global instead of a local for speed}
myScriptingComponent := OpenDefaultComponent(kOSAComponentType, kOSAGenericscriptingComponentSubtype);
{load script… create a context}
iErr := OSALoad(myScriptingComponent, scriptDesc, kOSAModeNull, scriptID);
{create an address descriptor describing the current application}
if iErr = noErr then
begin
myPSN.highLongOfPSN := 0;
myPSN.lowLongOfPSN := kCurrentProcess;
iErr := AECreateDesc(typeProcessSerialNumber, @myPSN, SizeOf(myPSN), addressDesc);
end;
{create AEs for the sent and reply data, with the Class and ID of the handler within the script}
{NOTE that the reply event must be pre-built, or there is nowhere for the reply data to go. It doesn't much matter what's}
{inside it, as long as it's valid (has a memory allocation for the dataHandle)}
if iErr = noErr then
iErr := AECreateAppleEvent(class, ID, addressDesc, kAutoGenerateReturnID, kAnyTransactionID, ae);
if iErr = noErr then
iErr := AECreateAppleEvent(class, ID, addressDesc, kAutoGenerateReturnID, kAnyTransactionID, reply);
{add the parameter data for the script handler. Use the direct parameter '----'}
if iErr = noErr then
iErr := AEPutKeyDesc(ae, keyDirectObject, sentData);
{execute the script handler within the script by sending the script context (AKA Script Object) an event}
if iErr = noErr then
iErr := OSADoEvent(myScriptingComponent, ae, scriptID, kOSAModeNull, reply);
{extract the data from the reply descriptor}
if iErr = noErr then
iErr := AEGetKeyDesc(reply, keyDirectObject, typeWildCard, replyData);
{NOTE: clean up, but DON'T dispose of scriptDesc, as it' s really a resource handle, and will be disposed of as such, later}
ie := AEDisposeDesc(addressDesc);
ie := AEDisposeDesc(ae);
ie := AEDisposeDesc(reply);
ignoreErr := OSADispose(myScriptingComponent, scriptID);
end;
{dispose the memory used by the script resource}
ReleaseResource(scriptData);
{close the connection with the AppleScript component}
ignoreErr := CloseComponent(myScriptingComponent);
ExecuteEventInCompiledScriptResource := iErr;
end;
end.